package org.study.jdbc.boot;

import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.study.jdbc.boot.dao.StudentService;
import org.study.jdbc.boot.model.Student;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@SpringBootTest
class JdbcBootApplicationTests02 {
    @Autowired
    @Qualifier("studentServiceP6spy")
    private StudentService studentService;
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

    @Test
    void testInsert() {
        Student student = new Student();
        student.setId(Long.valueOf(formatter.format(LocalDateTime.now())));
        student.setName("测试");
        student.setGender(1);
        student.setGrade(1);
        student.setScore(90D);
        int insert1 = studentService.insertWithId(student);
        System.out.println("新增结果1：" + insert1);
        int insert2 = studentService.insertWithoutId(student);
        System.out.println("新增结果2：" + insert2);
    }

    @Test
    void testInsertBatch() {
        List<Student> list = Lists.newArrayList();
        for (int i = 1; i <= 5; i++) {
            Student student = new Student();
            student.setId(Long.valueOf(formatter.format(LocalDateTime.now())) + i);
            student.setName("测试-" + i);
            student.setGender(i % 2);
            student.setGrade(i % 3);
            student.setScore(i * 10D);
            list.add(student);
        }
        int[] insert = studentService.insertBatch(list);
        System.out.println("批量新增结果：" + JSON.toJSONString(insert));
    }

    @Test
    void testUpdate() {
        Student student = new Student();
        student.setId(1L);
        student.setName("更新-测试");
        int update = studentService.updateNameById(student);
        System.out.println("更新结果：" + update);

        /*int delete = studentDao.deleteById(1L);
        System.out.println("删除结果：" + delete);*/
    }

    @Test
    void testSelect() {
        List<Student> selectByNameList = studentService.selectByName("测试");
        System.out.println("查询结果1：" + JSON.toJSONString(selectByNameList));

        List<Student> studentList = studentService.selectAll();
        System.out.println("查询结果2：" + JSON.toJSONString(studentList));
    }

    @Test
    void testTransactionWithoutXa() {
        Student student4Insert = new Student();
        student4Insert.setId(1L);
        student4Insert.setName("事务测试-新增");
        student4Insert.setGender(1);
        student4Insert.setGrade(1);
        student4Insert.setScore(90D);

        Student student4Update = new Student();
        student4Update.setId(2L);
        // student4Update.setName("事务测试-更新");

        studentService.transactionWithoutXa(student4Insert, student4Update, 2);
    }

    @Test
    void testTransactionWithXa() {
        Student student4Insert = new Student();
        student4Insert.setId(1L);
        student4Insert.setName("XA事务测试-新增");
        student4Insert.setGender(1);
        student4Insert.setGrade(1);
        student4Insert.setScore(90D);

        Student student4Update = new Student();
        student4Update.setId(2L);
        student4Update.setName("XA事务测试-更新");

        studentService.transactionWithXa(student4Insert, student4Update, true);
    }
}
